//
//  LeftViewController.m
//  Slice
//
//  Created by lanou on 16/6/13.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "LeftViewController.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>
//用户头像
@property (nonatomic, strong) UIImageView *userImgView;




@end

@implementation LeftViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
    NSLog(@"%@",dic[@"uname"]);
    if (dic) {
        [self.btn setTitle:dic[@"uname"] forState: UIControlStateNormal];
    } else {
        [self.btn setTitle:@"登陆/注册" forState: UIControlStateNormal];

    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [[NSArray alloc] initWithObjects:@"阅读",@"电台",@"话题",@"良品",@"收藏", nil];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 160, self.view.frame.size.width, self.view.frame.size.height - 160) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor = [UIColor blackColor];

    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"321"];
//    出来的时候选中第一行
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:1] animated:YES scrollPosition:UITableViewScrollPositionNone];

    self.VCArr = [[NSMutableArray alloc] initWithObjects:@"ReadViewController",@"RadioViewController",@"TopicViewController",@"ProductViewController",@"StoreTableViewController", nil];
    
    
    //初始化头像
    self.userImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 50, 50)];
    self.userImgView.backgroundColor = [UIColor blueColor];
    self.userImgView.image = [UIImage imageNamed:@"userDefult"];
    self.userImgView.layer.cornerRadius = 25;
    [self.view addSubview:self.userImgView];
    
//    登陆按钮
   self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
   self.btn.frame = CGRectMake(65, 40, 80, 50);
    self.btn.backgroundColor = [UIColor whiteColor];
    self.btn.layer.borderColor = [UIColor redColor].CGColor;
    self.btn.layer.borderWidth = 3;
    self.btn.layer.cornerRadius = 5;
    [self.view addSubview:self.btn];
    [self.btn addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
}

//登陆按钮动作
- (void)loginAction {
    UINavigationController *navc = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"navagation"];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"user"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"你要注销吗?" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//            s删除用户数据
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user"];
//            登陆按钮标题改回来
            [self.btn setTitle:@"登陆/注册" forState:UIControlStateNormal];
            
        }];
        
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    
        }];
        
        [alert addAction:action1];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
        
    } else {
    [self presentViewController:navc animated:YES completion:nil];
    }
}









- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"321"];
    cell.textLabel.text = self.arr[indexPath.row];
    cell.layer.borderColor = [UIColor redColor].CGColor;
    cell.layer.borderWidth = 0.5;
    cell.layer.cornerRadius = 5;
    if (self.tableView.indexPathForSelectedRow == indexPath) {
        cell.backgroundColor = [UIColor greenColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.index == indexPath ) {
        return;
    }
    self.index = indexPath;
    
    NSString *className = self.VCArr[indexPath.row];
    
//    从字典中寻找需要的控制器.如果找不到就创建新的
    AppDelegate *dele = [UIApplication sharedApplication].delegate;
    UINavigationController *navc =  [dele.dicVC objectForKey:className];
    if (navc == nil || navc.view == nil) {
        //    实例化这个类
        UIViewController *vc = [[NSClassFromString(className) alloc] init ];
        //    添加导航控制器
        navc = [[UINavigationController alloc]initWithRootViewController:vc];
        //    添加按钮
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:[UIApplication sharedApplication].delegate action:@selector(action)];
        
        vc.navigationItem.leftBarButtonItem = item;
//        将初始化来的控制器放入字典
        [dele.dicVC setObject:navc forKey:className];
        
    }
    //    设置为抽屉的主页
    [dele.vc setMainViewController:navc];

    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
